package cn.com.inossem.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.com.inossem.adapter.InputAdapter;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.RetrieveInfoModel;
import cn.com.inossem.utils.ActivityStackControlUtil;

import com.foxykeep.datadroid.requestmanager.Request;

/**
 * 
 * 检索向导
 * 
 * <p>
 * detailed comment
 * 
 * @author szc 2015-8-24
 * @see
 * @since 1.0
 */
public class InputActivity extends RequestActivity {
	int type = 0;
	String titlename = "";

	ListView listview;
	InputAdapter ia;
	List<RetrieveInfoModel> list;
	EditText editText;
	ImageView back;
	TextView titletext, savedata, tishi;
	String tishitext;

	public int position = 0;

	public InputActivity() {
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_tab_layout);

		initView();

	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		type = getIntent().getIntExtra("type", 0);
		position = getIntent().getIntExtra("position", 0);
		titlename = getIntent().getStringExtra("titlename");
		tishitext = getIntent().getStringExtra("tishitext");

		savedata = (TextView) this.findViewById(R.id.savedata);
		savedata.setOnClickListener(this);

		list = new ArrayList<RetrieveInfoModel>();
		titletext = (TextView) this.findViewById(R.id.titletext);
		tishi = (TextView) this.findViewById(R.id.tishi);
		switch (type) {
		case 0:
			break;
		case 1:
			titletext.setText("教育背景");
			tishi.setText("哪里曾经是大师的摇篮");
			break;
		case 2:
			titletext.setText(titlename);
			break;
		case 3:
			titletext.setText(titlename);
			tishi.setText(tishitext);
			break;

		default:
			break;
		}
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		editText = (EditText) this.findViewById(R.id.editText);
		editText.addTextChangedListener(textWatcher);

		listview = (ListView) this.findViewById(R.id.listview);
		ia = new InputAdapter(this, list, type);
		listview.setAdapter(ia);

	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.input_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			setResult(2);
			finish();
			break;
		case R.id.savedata:
			Intent intent = new Intent();
			intent.putExtra("nameid", "");
			System.out.println("editText.getText().toString():"
					+ editText.getText().toString());
			intent.putExtra("name", editText.getText().toString());
			setResult(10010, intent);
			finish();
			break;

		default:
			break;
		}
	}

	private TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			Log.d("TAG", "afterTextChanged--------------->");
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			Log.d("TAG", "beforeTextChanged--------------->");
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			Log.d("TAG", "onTextChanged--------------->");
			if (type == 2)
				return;
			String pid = "144";
			switch (type) {
			case 0:
				pid = "in (144,145)";
				break;
			case 1:
				pid = "= 10998";
				break;
			case 2:
				pid = "= 144";
				break;
			case 3:
				pid = "= 144";
				break;
			default:
				break;
			}
			if (!editText.getText().toString().equals("")) {
				selectData_info("t_dictionary", "pid " + pid
						+ " and (name_cn LIKE '%" + editText.getText().toString() + "%'"
						+ " or name_en LIKE '%" + editText.getText().toString() + "%')");
				ia.setList(list);
				ia.notifyDataSetChanged();

				System.out.println("ppppididid:" + pid);
			} else {
				list = new ArrayList<RetrieveInfoModel>();
				ia.notifyDataSetChanged();
			}
		}
	};

	private void selectData_info(String tabname, String selection) {
		list = new ArrayList<RetrieveInfoModel>();
		DatabaseHelper dh = new DatabaseHelper(this);
		SQLiteDatabase db = dh.getReadableDatabase();
		Cursor cursor = db.query(tabname, null, selection, null, null, null,
				"orders");
		while (cursor.moveToNext()) {
			RetrieveInfoModel rim = new RetrieveInfoModel();
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name_cn"));

			rim.setId(id);
			rim.setName(name);

			list.add(rim);
		}
		System.out.println("list:" + list.size());
		cursor.close();
		db.close();
		dh.close();

	}

}
